"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 故障&可靠性测试
Case Name   : 多次主备数据库switchover，运行TPCC（DTS2020052814956）；
Create At   : 2020/07/20
@zou_jialiang0505328114
Description :
    1、执行TPCC
    2、多次进行switchover
    3、再次进行TPCC
Expect      :
    1、执行TPCC成功
    2、多次进行switchover成功
    3、再次进行TPCC成功
History     :
    modified: 2021/01/13  modified by @wan005,switchover前增加查询，
    modified: 2021/01/19  modified by @wan005,调整等待主备一致性位置
    modified：2021/02/02  modified by @peilinqian，tpcc执行过程中，等待主备同步后，
        结果断言tpcc执行失败不符合预期；且与dts缺陷场景存在出入；修改整个用例逻辑
"""
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import *
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class DbsysCase159(unittest.TestCase):
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('----this is setup----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.sta_1_sh = CommonSH('Standby1DbUser')
        self.sta_2_sh = CommonSH('Standby2DbUser')
        self.pri_dbuser = Node(node='PrimaryDbUser')
        self.pri_root = Node(node='PrimaryRoot')
        self.sta1_dbuser = Node(node='Standby1DbUser')
        self.sta1_root = Node(node='Standby1Root')
        self.com = Common()
        self.constant = Constant()
        self.log.info('----Opengauss_Reliability_Dbsys_Case159 start----')

    def test_main(self):
        self.log.info('----1. 运行tpcc----')
        tpcc_result = self.com.startTPCC(self.pri_root, macro.TPCC_PATH)
        self.log.info('----' + tpcc_result + '----')
        self.log.info(tpcc_result)
        self.assertIn(self.constant.TPCC_SUCCESS_MSG, tpcc_result)

        self.log.info('----同步是否正常----')
        self.pri_sh.check_location_consistency('primary', 3)
        self.log.info('----查询主备状态----')
        status = self.pri_sh.getDbClusterStatus('status')
        self.assertTrue(status)

        self.log.info('---2. 多次进行switchover----')
        for i in range(10):
            self.log.info(f'----round {i}----')
            success_msg = self.constant.SWITCH_SUCCESS_MSG
            switch_msg = self.sta_1_sh.executeGsctl('switchover', success_msg)
            self.assertTrue(switch_msg)
            refresh_msg = self.sta_1_sh.exec_refresh_conf()
            self.assertTrue(refresh_msg)

            switch_msg = self.pri_sh.executeGsctl('switchover', success_msg)
            self.assertTrue(switch_msg)
            refresh_msg = self.pri_sh.exec_refresh_conf()
            self.assertTrue(refresh_msg)

            result = self.pri_sh.check_location_consistency('primary', 3)
            self.assertTrue(result)

        self.log.info('----同步是否正常----')
        self.pri_sh.check_location_consistency('primary', 3)
        self.log.info('----查询主备状态----')
        status = self.pri_sh.getDbClusterStatus('status')
        self.assertTrue(status)

        self.log.info('----3、再次执行tpcc----')
        tpcc_result = self.com.startTPCC(self.pri_root, macro.TPCC_PATH)
        self.log.info('----' + tpcc_result + '----')
        self.log.info(tpcc_result)
        self.assertIn(self.constant.TPCC_SUCCESS_MSG, tpcc_result)

        self.log.info('----同步是否正常----')
        self.pri_sh.check_location_consistency('primary', 3)
        self.log.info('----查询主备状态----')
        status = self.pri_sh.getDbClusterStatus('status')
        self.assertTrue(status)

    def tearDown(self):
        self.log.info('----Opengauss_Reliability_Dbsys_Case159 执行结束----')
